#! /bin/sh
#
# @@@ START COPYRIGHT @@@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#
# @@@ END COPYRIGHT @@@
#
######################################################################
# This script stops the QMM process, which also causes its child QMP
# and QMS processes to go away. Once terminated, QMM (and its children)
# can subsequently be restarted using the qmmstart script.
#-----------------------------------------------------------------------
#
####################################################################

# qmm name
qmmName="ZQM0000"

# The script can start from anywhere
thome=`pwd`

# who is the current user
currentUser=`whoami`

# script name
qmmscr="$thome/stopQmm"

# Output file
qmmlog="$thome/stopQmm.log"

NULL=/dev/null

# by default we assume we are running on a cluster
thisIsACluster=1

# check if this is a real cluster
function checkCluster
{
  if [ -f $SQ_PDSH ]; then
     thisIsACluster=1
     echo " "
     echo "running the $0 script in a cluster ....."
  else
     thisIsACluster=0
     echo " "
     echo "running the $0 script in a workstation ....."
  fi
}

function stopQMM
{
  # Remove script and its LOG file.
  rm $qmmlog > /dev/null 2>&1
  rm $qmmscr > /dev/null 2>&1

  echo " " | tee -a $qmmlog
  echo "checking if tdm_arkqmm is running." |  tee -a $qmmlog
  if [[ $thisIsACluster = 1 ]]; then
    sqps | grep -a $qmmName  > /dev/null 2>&1
  else
    ps -fu $currentUser | grep -a $qmmName | grep -v grep > /dev/null 2>&1
  fi

  result=$?
  # to make screen msgs look better
  echo " " | tee -a $qmmlog

  # 0 means at least one match. 1 means no  match. 2 means error
  if [[ $result != 0 ]]; then
    echo "tdm_arkqmm persistent process $qmmName is already stopped..."  |  tee -a $qmmlog
    echo " " | tee -a $qmmlog
  else
    stop_qmm;
    rm $qmmscr > /dev/null 2>&1
  fi
}

function stop_qmm
{
  echo "Stopping tdm_arkqmm persistent process. Please wait...."

  # construct script
  cat > $qmmscr << EOF
  sqshell -a <<eof
  persist kill QMN
  exit
eof
EOF

  # run  stopQMM.ksh
  chmod 777 $qmmscr
  sh $qmmscr > $qmmlog 2>&1

  # to make screen msgs look better
  echo " " | tee -a $qmmlog

  # verify persistent process $qmmName stopped successfully
  sleep 5
  if [[ $thisIsACluster = 1 ]]; then
    sqps | grep $qmmName > /dev/null 2>&1
  else
    ps -fu $currentUser | grep $qmmName | grep -v grep > /dev/null 2>&1
  fi

  result=$?

  #Check if return value of grep,
  #   0 is at least one match found,
  #   1 is no matches found,
  #   2 is error

  if [[ $result = 1 ]];then
     echo "tdm_arkqmm process $qmmName stopped successfully." | tee -a $qmmlog
  else
     echo "Failed to stop tdm_arkqmm persistent process."
     echo "Please see $qmmlog for more details."
  fi

  # to make screen msgs look better
  echo " " | tee -a $qmmlog
}


####################################################################
#                                                                  #
#                  Main Script starts here                         #
#                                                                  #
####################################################################
TITLEBAR=$0;

#check where we are running this script
checkCluster;

# Call the function to shut down QMM.
stopQMM;

